//
//  BottomBoxView.m
//  Customized
//
//  Created by AceWei on 2017/6/30.
//  Copyright © 2017年 AceWei. All rights reserved.
//

#import "BottomBoxView.h"
#import "SQSectorButton.h"
#import "FootReportConstantKeyHeader.h"
#import "UIColor+Hex.h"

#import "BasicShoesSpu.h"

#import "AdviseCollectionViewCell.h"
#import "RightShoesCollectionViewCell.h"

#import <Masonry.h>
#import <MJExtension.h>


@interface BottomBoxView () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;


@property (nonatomic, strong) SQSectorButton *footAdviseButton;
@property (nonatomic, strong) SQSectorButton *suitableShoesButton;



@property (nonatomic, copy) NSArray *RightShoesDataSource;
@property (nonatomic, copy) NSArray *AdviseDataSource;



@end

@implementation BottomBoxView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupDataSource];
        [self setupUI];
    }
    return self;
}


#pragma mark - DataSource
- (void)setupDataSource
{
//    if ([self.delegate respondsToSelector:@selector(addAdviseDataSource)]) {
//        self.AdviseDataSource = [self.delegate addAdviseDataSource];
//    }
    
    if ([self.delegate respondsToSelector:@selector(addRightShoesDataSource)]) {
        self.RightShoesDataSource = [self.delegate addRightShoesDataSource];
    }
    
//    NSAssert(self.AdviseDataSource == nil, @"self.AdviseDataSource == nil is error!");
    NSAssert(self.RightShoesDataSource == nil, @"self.RightShoesDataSource == nil is error!");
}


#pragma mark - UI（设置各种UI）
- (void)setupUI
{
    [self createBorderImageView];
    
    [self createFootAdviseButton];
    
    [self createSuitableShoesButton];
    
    [self createCollectView];
}


- (void)createCollectView
{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake(124, 124);
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
    [self addSubview:self.collectionView];
    
    
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        CGFloat padding = 15;
        make.left.offset(padding);
        make.right.bottom.offset(-padding);
        make.top.equalTo(self.footAdviseButton.mas_bottom).offset(10);
    }];
}



- (void)createBorderImageView
{
    UIImageView * imageView = [[UIImageView alloc]init];
    imageView.image = [UIImage imageNamed:@"footReport_TipsGreenRectangle"];
    [self insertSubview:imageView atIndex:0];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
}



/**
 *  adviseButton&recommendButton
 */
- (void)createFootAdviseButton
{
    self.footAdviseButton = [[SQSectorButton alloc]init];
    [self addSubview:self.footAdviseButton];
    
    [self.footAdviseButton setTitle:@"适合鞋款" forState:UIControlStateNormal];
    
    self.footAdviseButton.selected = YES;
    
    [self configureBottomViewButton:self.footAdviseButton];
    
    [self.footAdviseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.mas_top).offset(-1.85);;
        make.trailing.mas_equalTo(self).offset(-23);
        make.width.mas_equalTo(117.5);
        make.height.mas_equalTo(30);
        
    }];
}


- (void)createSuitableShoesButton
{
    self.suitableShoesButton = [[SQSectorButton alloc] init];
    [self addSubview:self.suitableShoesButton];
    
    [self.suitableShoesButton setTitle:@"护足建议" forState:UIControlStateNormal];
    [self configureBottomViewButton:self.suitableShoesButton];
    
    [self.suitableShoesButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.trailing.mas_equalTo(self.footAdviseButton.mas_leading).offset(25);
        make.top.mas_equalTo(self.mas_top).offset(-1.85);
        make.width.mas_equalTo(117.5);
        make.height.mas_equalTo(30);
        
    }];
}



- (void)configureBottomViewButton:(UIButton *)sender
{
    sender.titleLabel.font = [UIFont fontWithName:KFootReportVCBottomViewButton_FontName size:KFootReportVCBottomViewButton_FontSize];
    [sender setTitleColor:[UIColor colorWithHexString:@"60C0C2"] forState:UIControlStateSelected];
    [sender setTitleColor:[UIColor colorWithHexString:@"FFFFFF"] forState:UIControlStateNormal];
    self.footAdviseButton.adjustsImageWhenHighlighted = NO;
    
    [sender setBackgroundImage:[UIImage imageNamed:@"footReport_BottomRectangleGreenBtn"] forState:UIControlStateNormal];
    [sender setBackgroundImage:[UIImage imageNamed:@"footReport_BottomRectangleWhiteBtn"] forState:UIControlStateSelected];
    
    [sender addTarget:self action:@selector(buttonChangeType:) forControlEvents:UIControlEventTouchUpInside];
}



#pragma mark - Event（点击事件）
#pragma mark 护足建议和适合鞋款切换
- (void)buttonChangeType:(UIButton *)sender
{
    self.footAdviseButton.selected = !self.footAdviseButton.selected;
    self.suitableShoesButton.selected = !self.suitableShoesButton.selected;
    
    //[self.collectionView reloadData];
}



#pragma mark - UICollectionView Delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.RightShoesDataSource.count;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
#if 0
    [collectionView registerClass:[AdviseCollectionViewCell class] forCellWithReuseIdentifier:@"adviseCell"];
    
    AdviseCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"adviseCell" forIndexPath:indexPath];
    
    cell.text = @"hello world";
    
#else
    [collectionView registerClass:[RightShoesCollectionViewCell class] forCellWithReuseIdentifier:@"rightCell"];
    
    RightShoesCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"rightCell" forIndexPath:indexPath];
    
    
    
    BasicShoesSpu *basicShoesSpu = [BasicShoesSpu mj_objectWithKeyValues:self.RightShoesDataSource[indexPath.row]];
    
    cell.model = basicShoesSpu;
    
    
    return cell;
    
#endif
    
    
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld", indexPath.row);
}


@end
